﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class TranslationDAL : BaseStorageDAL, ITranslationDAL
	{
		#region ITranslationDAL Members

		public void CreateTranslation(Model.Translation translation, System.Data.IDbTransaction transaction)
		{
			dtoXmlStorage.TranslationRow translationRow = Storage.Translation.FindById(translation.Id);

			bool isNew = (translationRow == null);
			bool isChanged = false;

			if (isNew)
			{
				translationRow = Storage.Translation.NewTranslationRow();
				isChanged = true;
			}

			if (!isChanged)
			{

				isChanged |= !translation.Id.Equals(translationRow.Id);
				if (!translationRow.IsProgressNull())
					isChanged |= !translation.Progress.Equals(translationRow.Progress);
				if ((!translationRow.IsCommentNull())&&(!String.IsNullOrEmpty(translation.Comment)))
					isChanged |= !translation.Comment.Equals(translationRow.Comment);
				if ((!translationRow.IsCustomerIdentityNull()) && (!String.IsNullOrEmpty(translation.CustomerIdentity)))
					isChanged |= !translation.CustomerIdentity.Equals(translationRow.CustomerIdentity);
				if ((!translationRow.IsTPIdentityNull()) && (!String.IsNullOrEmpty(translation.TPIdentity)))
					isChanged |= !translation.TPIdentity.Equals(translationRow.TPIdentity);
				if ((!translationRow.IsStatusNull())&&(!String.IsNullOrEmpty(translation.Status)))
					isChanged |= !translation.Status.Equals(translationRow.Status);

				if (translation.Responsible != null)
				{
					if (!translationRow.IsResponsibleIdNull())
						isChanged |= !translation.Responsible.Id.Equals(translationRow.ResponsibleId);
				}

				if (translation.Project != null)
				{
					if (!translationRow.IsProjectIdNull())
						isChanged |= !translation.Project.Id.Equals(translationRow.ProjectId);
				}

				if (translation.LanguagePair != null)
				{
					if (translation.LanguagePair.SourceLanguage != null)
					{
						if (!translationRow.IsSourceLanguageCodeNull())
							isChanged |= !translation.LanguagePair.SourceLanguage.LanguageId.Equals(translationRow.SourceLanguageCode);
					}

					if (translation.LanguagePair.TargetLanguage != null)
					{
						if (!translationRow.IsTargetLanguageCodeNull())
							isChanged |= !translation.LanguagePair.TargetLanguage.LanguageId.Equals(translationRow.TargetLanguageCode);
					}
				}

				if (translation.SourceContent != null)
				{
					if (!translationRow.IsSourceContentIdNull())
						isChanged |= !translation.SourceContent.Id.Equals(translationRow.SourceContentId);
				}

				if (translation.TargetContent != null)
				{
					if (!translationRow.IsTargetContentIdNull())
						isChanged |= !translation.TargetContent.Id.Equals(translationRow.TargetContentId);
				}
			}


			translationRow.Id = translation.Id;
			translationRow.Progress = translation.Progress;
			translationRow.Comment = translation.Comment;
			translationRow.CustomerIdentity = translation.CustomerIdentity;
			translationRow.TPIdentity = translation.TPIdentity;
			translationRow.Status = translation.Status;

			if (translation.Responsible != null)
			{
				translationRow.ResponsibleId = translation.Responsible.Id;
			}

			if (translation.Project != null)
			{
				translationRow.ProjectId = translation.Project.Id;
			}


			if (translation.LanguagePair != null)
			{
				if (translation.LanguagePair.SourceLanguage != null)
				{
					translationRow.SourceLanguageCode = translation.LanguagePair.SourceLanguage.LanguageId;
				}

				if (translation.LanguagePair.TargetLanguage != null)
				{
					translationRow.TargetLanguageCode = translation.LanguagePair.TargetLanguage.LanguageId;
				}
			}

			if (translation.SourceContent != null)
			{
				translationRow.SourceContentId = translation.SourceContent.Id;
			}

			if (translation.TargetContent != null)
			{
				translationRow.TargetContentId = translation.TargetContent.Id;
			}

			if (isNew)
			{
				Storage.Translation.AddTranslationRow(translationRow);
			}

			if (isChanged)
			{
				translationRow.TimeSpan = CurrentTimeSpan;
			}


			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		#endregion


		public IList<Translation> ReadTranslationsByProject(Project project)
		{
			List<Translation> result = new List<Translation>();

			foreach (dtoXmlStorage.TranslationRow translationRow in Storage.Translation)
			{
				if (string.Compare(translationRow.ProjectId, project.Id, true) == 0)
				{
					Translation translation = ReadTranslation(translationRow.Id);
					translation.Project = project;

					if (!translationRow.IsSourceContentIdNull())
						translation.SourceContent = project.FindContent(translationRow.SourceContentId);
					if (!translationRow.IsTargetContentIdNull())
						translation.TargetContent = project.FindContent(translationRow.TargetContentId);

					result.Add(translation);
				}
			}

			return result;
		}




		public Translation ReadTranslation(string translationId)
		{
			dtoXmlStorage.TranslationRow translationRow = Storage.Translation.FindById(translationId);

			Translation translation = new Translation();
			translation.Id = translationRow.Id;
			translation.Progress = translationRow.Progress;

			if (!translationRow.IsStatusNull())
				translation.Status = translationRow.Status;

			if (!translationRow.IsResponsibleIdNull())
				translation.Responsible = DALFacade.Instance.ContactDAL.ReadContact(translationRow.ResponsibleId);

			if (!translationRow.IsCommentNull())
				translation.Comment = translationRow.Comment;

			if (!translationRow.IsCustomerIdentityNull())
				translation.CustomerIdentity = translationRow.CustomerIdentity;

			if (!translationRow.IsTPIdentityNull())
				translationRow.TPIdentity = translationRow.TPIdentity;

			translation.Project = new Project();
			translation.Project.Id = translationRow.ProjectId;

			LanguagePair lp = new LanguagePair();
			lp.SourceLanguage = DALFacade.Instance.LanguageDAL.ReadLanguage(translationRow.SourceLanguageCode);
			lp.TargetLanguage = DALFacade.Instance.LanguageDAL.ReadLanguage(translationRow.TargetLanguageCode);
			translation.LanguagePair = lp;

			if (!translationRow.IsSourceContentIdNull())
			{
				translation.SourceContent = new ContentEntity();
				translation.SourceContent.Id = translationRow.SourceContentId;
			}

			if (!translationRow.IsTargetContentIdNull())
			{
				translation.TargetContent = new ContentEntity();
				translation.TargetContent.Id = translationRow.TargetContentId;
			}

			return translation;
		}


		public void UpdateTranslation(Translation translation, System.Data.IDbTransaction transaction)
		{
			CreateTranslation(translation, transaction);
		}

	}
}